package com.mingmen.canting.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mingmen.canting.R;
import com.mingmen.canting.defineview.GlideRoundTransform;
import com.mingmen.canting.httpconfig.AppConfig;
import com.mingmen.canting.model.CommentModel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.MyHolder> {
    private Context context;
    private List<CommentModel.DataBean.DetailsBean> data = new ArrayList<>();
    private OnMyAdapterCallBack callBack;

    public CommentAdapter(Context context, List<CommentModel.DataBean.DetailsBean> data) {
        this.context = context;
        this.data = data;
    }

    public void setData(List<CommentModel.DataBean.DetailsBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void setCallBack(OnMyAdapterCallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_photowai, null);
        return new MyHolder(view);
    }

    List<String> images = new ArrayList<>();

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        CommentModel.DataBean.DetailsBean dataBean = data.get(position);

        holder.text_commidyName.setText(dataBean.getRestaurantName());
        holder.recyclerView.setLayoutManager(new GridLayoutManager(context, 3));
//        holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
        holder.recyclerView.setAdapter(holder.childAdater);
//            childAdater.setData(data.get(position).getImgs());

 /*       List<CommentModel.DataBean.DetailsBean> detailsBeanList = new ArrayList<>();
        detailsBeanList = data.get(position).getDetails();
        for (int i = 0; i < detailsBeanList.size(); i++) {
            images.add(AppConfig.IMGURL+detailsBeanList.get(i).getPicture());
        }
*/
//        holder.childAdater.setData(detailsBeanList);


        Glide.with(context).load(AppConfig.IMGURL + dataBean.getPicture()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(context, 5)).into(holder.ivAvatar);

        Glide.with(context).load(AppConfig.IMGURL + dataBean.getPicture()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(context, 5)).into(holder.ivAvatar1);

        Glide.with(context).load(AppConfig.IMGURL + dataBean.getPicture()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(context, 5)).into(holder.ivAvatar);
//
//        holder.childAdater.setData(detailsBeanList);
        holder.childAdater.setOnChildClick(new CommentDetailAdapter.OnChildClick() {
            @Override
            public void onItemClick(int currentPosition) {
                if (callBack != null) {
//                        callBack.onItemClick(currentPosition, images.get(position).getImgs());
                    callBack.onItemClick(currentPosition, images);
                }
            }
        });
     /*   if (dataBean.getAnnouncement().equals("0")) {
            holder.anonymousFlag.setVisibility(View.GONE);
        } else {
            holder.anonymousFlag.setVisibility(View.VISIBLE);
        }*/
        holder.mark.setText(dataBean.getMark());

        BigDecimal bigDecimal = new BigDecimal(dataBean.getScore());
        BigDecimal bigDecimal0 = new BigDecimal("1");
        BigDecimal bigDecima20 = new BigDecimal("2");
        BigDecimal bigDecima30 = new BigDecimal("3");
        BigDecimal bigDecima40 = new BigDecimal("4");
        BigDecimal bigDecima50 = new BigDecimal("5");
        if (bigDecimal.compareTo(bigDecimal0) == 0) {
//10
            holder.img_score.setImageResource(R.mipmap.star_first);
        } else if (bigDecimal.compareTo(bigDecimal0) == -1) {
            holder.img_score.setImageResource(R.mipmap.star_first);
        } else if (bigDecimal.compareTo(bigDecimal0) == 1 && bigDecimal.compareTo(bigDecima20) == -1) {
            holder.img_score.setImageResource(R.mipmap.star_second);
        } else if (bigDecimal.compareTo(bigDecima20) == 0) {
//10
            holder.img_score.setImageResource(R.mipmap.star_first);
        } else if (bigDecimal.compareTo(bigDecima20) == 1 && bigDecimal.compareTo(bigDecima30) == -1) {
            holder.img_score.setImageResource(R.mipmap.star_three);
        } else if (bigDecimal.compareTo(bigDecima30) == 0) {
//10
            holder.img_score.setImageResource(R.mipmap.star_three);
        } else if (bigDecimal.compareTo(bigDecima30) == 1 && bigDecimal.compareTo(bigDecima40) == -1) {
            holder.img_score.setImageResource(R.mipmap.star_forth);
        } else if (bigDecimal.compareTo(bigDecima40) == 0) {
//10
            holder.img_score.setImageResource(R.mipmap.star_forth);
        } else if (bigDecimal.compareTo(bigDecima40) == 1 && bigDecimal.compareTo(bigDecima50) == -1) {
            holder.img_score.setImageResource(R.mipmap.star_fifth);
        } else if (bigDecimal.compareTo(bigDecima50) == 0) {
//10
            holder.img_score.setImageResource(R.mipmap.star_fifth);
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        RecyclerView recyclerView;

        TextView text_commidyName;
        CommentDetailAdapter childAdater = new CommentDetailAdapter(context);
        RecyclerView item_one_rv;
        ImageView ivAvatar;
        ImageView ivAvatar2;
        ImageView img_score;
        LinearLayout anonymousFlag;
        TextView mark;
        ImageView ivAvatar1;

        public MyHolder(@NonNull View itemView) {
            super(itemView);

            recyclerView = itemView.findViewById(R.id.item_one_rv);
            text_commidyName = itemView.findViewById(R.id.text_commidyName);
            item_one_rv = itemView.findViewById(R.id.item_one_rv);
            ivAvatar = itemView.findViewById(R.id.ivAvatar);
            ivAvatar2 = itemView.findViewById(R.id.ivAvatar2);
            img_score = itemView.findViewById(R.id.img_score);
            anonymousFlag = itemView.findViewById(R.id.anonymousFlag);
            mark = itemView.findViewById(R.id.mark);
            ivAvatar1= itemView.findViewById(R.id.ivAvatar1);
        }


    }

    public interface OnMyAdapterCallBack {
        void onItemClick(int position, List<String> itemImgs);
    }
}
